package com.eascs.notice.dao;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.multipart.MultipartFile;

import com.eascs.admin.common.config.FtpConfig;
import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.admin.common.utils.FtpNetUtils;
import com.eascs.admin.common.utils.StringUtils;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.common.exception.RuntimeIOException;
import com.eascs.common.util.UuidUtil;
import com.eascs.datalist.dao.O2oDataListDao;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.file.FileTypeConstants;
import com.eascs.notice.entity.OaDataListFileEntity;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

@Repository
public class O2oNoticeLetterDao extends BaseO2oDao {

	@Autowired
	O2oDataListDao o2oDataListDao;

	public String oaUploadFile(MultipartFile file, String datalistCode, String billCode) {
		User user = UserHolder.getUser();
		try {
			String extendName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();
			String yearmonth = StringUtils.getFourBitYearMonth(new Date());
			String datepath = yearmonth.substring(0, 2) + "/" + yearmonth.substring(2, 4) + "/";
			String randomstr = Long.toString(System.currentTimeMillis()) + StringUtils.getRandomString(2);
			String relatedir = datalistCode + "/" + datepath;
			String newfilename = randomstr + "." + extendName;
			FtpNetUtils.uploadFile(FtpConfig.ftpConfigSetting().getHref(), Integer.valueOf(FtpConfig.ftpConfigSetting().getPort()), FtpConfig.ftpConfigSetting().getUsername(), FtpConfig.ftpConfigSetting().getPassword(), relatedir, newfilename, file.getInputStream());
			List<OaDataListFileEntity> list = o2oDataListDao.findOaDateEntity(billCode, datalistCode);
			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					OaDataListFileEntity loanEntity = list.get(i);
					loanEntity.setUdpateTime(new Date());
					loanEntity.setUpdateUserName(user.getCname());
					loanEntity.setUpdateUserNo(user.getNo());
					loanEntity.setFileType(datalistCode);
					loanEntity.setOaBillCode(billCode);
					loanEntity.setExtendName(extendName);
					loanEntity.setOriginalName(file.getOriginalFilename());
					loanEntity.setUrlFilePath(relatedir + newfilename);
					this.updateById(loanEntity);
				}
			} else {
				OaDataListFileEntity loanEntity = new OaDataListFileEntity();
				loanEntity.setUdpateTime(new Date());
				loanEntity.setUpdateUserName(user.getCname());
				loanEntity.setUpdateUserNo(user.getNo());
				loanEntity.setFileType(datalistCode);
				loanEntity.setOaBillCode(billCode);
				loanEntity.setExtendName(extendName);
				loanEntity.setOriginalName(file.getOriginalFilename());
				loanEntity.setUrlFilePath(relatedir + newfilename);
				this.insert(loanEntity);
			}
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		}
		return "";
	}
	
	public String o2oUploadFile(MultipartFile file, String datalistCode, String billCode) {
		User user = UserHolder.getUser();
		try {
			String extendName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1)
					.toLowerCase();
			String yearmonth = StringUtils.getFourBitYearMonth(new Date());
			String datepath = yearmonth.substring(0, 2) + "/" + yearmonth.substring(2, 4) + "/";
			String randomstr = Long.toString(System.currentTimeMillis()) + StringUtils.getRandomString(2);
			String relatedir = datalistCode + "/" + datepath;
			String newfilename = randomstr + "." + extendName;
			FtpNetUtils.uploadFile(FtpConfig.ftpConfigSetting().getHref(),
					Integer.valueOf(FtpConfig.ftpConfigSetting().getPort()), FtpConfig.ftpConfigSetting().getUsername(),
					FtpConfig.ftpConfigSetting().getPassword(), relatedir, newfilename, file.getInputStream());
			List<O2oDataListEntity> list = o2oDataListDao.findDateEntity(billCode, datalistCode);
			if (list != null && list.size() > 0) {
				 if(datalistCode.equals(FileTypeConstants.DALIST_TYPE_OTH)) {
					O2oDataListEntity loanEntity = new O2oDataListEntity();
					loanEntity.setCreatDate(new Date());
					loanEntity.setUrlpath(relatedir + newfilename);
					loanEntity.setOriginalName(file.getOriginalFilename());
					loanEntity.setDatalistCode(datalistCode);
					loanEntity.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
					loanEntity.setCreatId(user.getNo());
					loanEntity.setCreatOr(user.getCname());
					loanEntity.setExtendNmae(extendName);
					loanEntity.setLoanapplyNo(billCode);
					loanEntity.setIsDelete(StatusContexts.O2O_NO);
					loanEntity.setDatalistNo(UuidUtil.generateShortUuid(4));
					this.insert(loanEntity);
				}else{
					for (int i = 0; i < list.size(); i++) {
						O2oDataListEntity loanEntity=list.get(i);
						loanEntity.setUrlpath(relatedir + newfilename);
						loanEntity.setOriginalName(file.getOriginalFilename());
						loanEntity.setDatalistCode(datalistCode);
						loanEntity.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
						loanEntity.setCreatId(user.getNo());
						loanEntity.setCreatOr(user.getCname());
						loanEntity.setExtendNmae(extendName);
						loanEntity.setLoanapplyNo(billCode);
						loanEntity.setIsDelete(StatusContexts.O2O_NO);
						this.updateById(loanEntity);
					}
				}
			} else {
				O2oDataListEntity loanEntity = new O2oDataListEntity();
				loanEntity.setCreatDate(new Date());
				loanEntity.setUrlpath(relatedir + newfilename);
				loanEntity.setOriginalName(file.getOriginalFilename());
				loanEntity.setDatalistCode(datalistCode);
				loanEntity.setDatalistName(FileTypeConstants.getDatalistName(datalistCode));
				loanEntity.setCreatId(user.getNo());
				loanEntity.setCreatOr(user.getCname());
				loanEntity.setExtendNmae(extendName);
				loanEntity.setLoanapplyNo(billCode);
				loanEntity.setDatalistNo(UuidUtil.generateShortUuid(4));
				loanEntity.setIsDelete(StatusContexts.O2O_NO);
				this.insert(loanEntity);
			}
		} catch (IOException e) {
			throw new RuntimeIOException(e);
		}
		return "";
	}
}
